package com.zang.blogz.codegenerator;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;

import java.sql.*;
import java.util.*;

/**
 * @author 臧臧 on 2022/6/17
 */
public class MPCodeGenerator {
    public static Map getDatabaseNameToTableNameAndColumnName(String databaseName) throws SQLException, ClassNotFoundException {
        String driver = "com.mysql.cj.jdbc.Driver";
        String url = "jdbc:mysql://116.62.45.3:3306/" + databaseName;
        String username = "root";
        String password = "zangzang";
        Map<String, Object> tableNameMap = new HashMap<>();
        //加载驱动
        Class.forName(driver);
        //获得数据库连接
        Connection connection = DriverManager.getConnection(url, username, password);
        //获得元数据
        DatabaseMetaData metaData = connection.getMetaData();
        //获得表信息
        ResultSet tables = metaData.getTables(null, null, null, new String[]{"TABLE"});

        while (tables.next()) {
            Map<String, String> columnNameMap = new HashMap<>(); //保存字段名

            //获得表名
            String table_name = tables.getString("TABLE_NAME");
            //通过表名获得所有字段名
            ResultSet columns = metaData.getColumns(null, null, table_name, "%");
            //获得所有字段名
            while (columns.next()) {
                //获得字段名
                String column_name = columns.getString("COLUMN_NAME");
                //获得字段类型
                String type_name = columns.getString("TYPE_NAME");

                columnNameMap.put(column_name, type_name);
            }

            tableNameMap.put(table_name, columnNameMap);

        }

        return tableNameMap;
    }
    public static void main(String[] args) throws SQLException, ClassNotFoundException {
        //HashMap<String, Object> computer_mall = (HashMap<String, Object>) getDatabaseNameToTableNameAndColumnName("blog");
        //Set<String> tableNames = computer_mall.keySet();
        //System.out.println("一共有表：" + tableNames.size() + "张");
        //List<String> tables = new ArrayList<>(tableNames);
        ArrayList<String> tables = new ArrayList<>();
        tables.add("tb_v");
        FastAutoGenerator.create("jdbc:mysql://localhost:3306/blog?serverTimezone=Asia/Shanghai&allowMultiQueries=true", "root", "zangzang")
                .globalConfig(builder -> {
                    builder.author("朵橙i") // 设置作者
                            .enableSwagger() // 开启 swagger 模式
                            .fileOverride() // 覆盖已生成文件
                            .outputDir(System.getProperty("user.dir")+"\\src\\main\\java") // 指定输出目录
                            .commentDate("yyyy-MM-dd");
                })
                .packageConfig(builder -> {
                    builder.parent("com.zang")
                            .moduleName("blogz")
                            .entity("entity")
                            .service("service")
                            .serviceImpl("service.impl")
                            .controller("controller")
                            .mapper("mapper")
                            .xml("mapperXml")
                            .pathInfo(Collections.singletonMap(OutputFile.xml,System.getProperty("user.dir")+"\\src\\main\\resources\\mapper\\"));
                })
                .strategyConfig(builder -> {
                    builder.addInclude(tables)
                            .addTablePrefix("tb_")
                            .serviceBuilder()
                            .formatServiceFileName("%sService")
                            .formatServiceImplFileName("%sServiceImpl")
                            .entityBuilder()
                            .enableLombok()
                            .enableTableFieldAnnotation()
                            .controllerBuilder()
                            .formatFileName("%sController")
                            .enableRestStyle()
                            .mapperBuilder()
                            .superClass(BaseMapper.class)
                            .formatMapperFileName("%sMapper")
                            .enableMapperAnnotation()
                            .formatXmlFileName("%sMapper");

                })
                .execute();
    }
}